package com.useeinfo.oa.open.admin.maintaining;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderLogBiz;
import com.useeinfo.oa.modules.maintaining.entity.MaintainOrder;
import com.useeinfo.oa.modules.maintaining.entity.MaintainOrderLog;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/web/admin/maintainLog")
public class OrderLogAction {
    @Autowired
    private MaintainOrderLogBiz maintainOrderLogBiz;
    @Autowired
    private MaintainOrderBiz maintainOrderBiz;

    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/admin/maintainOrder/logList");
        return modelAndView;
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJson(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                               @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                               @RequestParam(value = "name", required = false) String name,
                               @RequestParam(value = "startTime", required = false) String startTimeParam,
                               @RequestParam(value = "endTime", required = false) String endTimeParam,
                               HttpServletRequest request) {
        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("name", name);
        queryParam.getSqlMap().put("startTime", startTimeParam);
        queryParam.getSqlMap().put("endTime", endTimeParam);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (enterprise != null) {
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId() + "");
        }
        return maintainOrderLogBiz.findJSONList(queryParam);
    }

    /**
     * 根据操作类型获取详情
     *
     * @param orderId
     * @param type
     * @param status
     * @param request
     * @return
     */
    @RequestMapping("/getLogInfo")
    @ResponseBody
    public ResultDto getLogInfo(@RequestParam(value = "orderId", required = false) String orderId,
                                @RequestParam(value = "type", required = false) String type,
                                @RequestParam(value = "status", required = false) String status,

                                HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("maintainOrderId", orderId);
        queryParam.getSqlMap().put("orderOperationEnum", type);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (enterprise != null) {
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId() + "");
        }
        List<MaintainOrderLog> maintainOrderLogList = maintainOrderLogBiz.findList(queryParam);
        String person = "";
        String time = "";
        if (CollectionUtils.isNotEmpty(maintainOrderLogList)) {
            if (status.equals("first")) {
                person = maintainOrderLogList.get(maintainOrderLogList.size() - 1).getSystemUser().getUserName();
                time = DateTimeUtils.formatDateToStringWithTime(maintainOrderLogList.get(maintainOrderLogList.size() - 1).getCreateDate());
            } else {
                person = maintainOrderLogList.get(0).getSystemUser().getUserName();
                time = DateTimeUtils.formatDateToStringWithTime(maintainOrderLogList.get(0).getCreateDate());
            }
        }
        if (type.equals("RELEASE_ORDER") && CollectionUtils.isEmpty(maintainOrderLogList)) {
            MaintainOrder maintainOrder = maintainOrderBiz.findModel(StringConverters.null2Long(orderId));
            person = maintainOrder.getCreateUser().getUserName();
            time = DateTimeUtils.formatDateToStringWithTime(maintainOrder.getCreateDate());
        }
        jsonObject.put("person", person);
        jsonObject.put("timing", time);

        return ResultUtil.success(jsonObject);
    }
}
